package com.qigao.mall.web.controller.app.merchant;

import com.qigao.mall.api.ItemCatService;
import com.qigao.mall.commons.dto.ItemCatCreateDto;
import com.qigao.mall.commons.dto.ItemCatModiDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.web.controller.app.user.AppItemCatController;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.ItemCatCreateVo;
import com.qigao.mall.web.vo.app.ItemCatModiVo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app/merchant/itemCat")
@Api(tags = "App商户端商品分类相关API",value = "App商户端商品分类相关API")
public class MerchantItemCatController extends AppItemCatController {

    private static Logger logger= LoggerFactory.getLogger(MerchantItemCatController.class);
    @Resource
    ItemCatService itemCatService;

    //新增商品分类
    @PostMapping("/create")
    public Result create(@RequestBody ItemCatCreateVo itemCatCreateVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(itemCatCreateVo);
        try{
            ItemCatCreateDto itemCatCreateDto=new ItemCatCreateDto();
            BeanUtils.copyProperties(itemCatCreateVo,itemCatCreateDto);
            itemCatCreateDto.setItemSort(Integer.valueOf(itemCatCreateVo.getItemSort()));
            itemCatCreateDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            itemCatService.create(itemCatCreateDto);
            return Result.newSuccess();
        }catch (Exception e){

            logger.error("新增商品分类发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //修改商品分类
    @PostMapping("/modi")
    public Result create(@RequestBody ItemCatModiVo itemCatModiVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(itemCatModiVo);
        try{
            ItemCatModiDto itemCatModiDto=new ItemCatModiDto();
            BeanUtils.copyProperties(itemCatModiVo,itemCatModiDto);
            itemCatModiDto.setItemSort(Integer.valueOf(itemCatModiVo.getItemSort()));
            itemCatModiDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            itemCatService.modi(itemCatModiDto);
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("新增商品分类发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //删除商品分类
    @PostMapping("/del/{itemCatNo}")
    public Result create(@PathVariable("itemCatNo")String itemCatNo){
        return itemCatService.deleteByItemCatNo(itemCatNo);
    }


}
